package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.Miner22ShareBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.CollectBean
import com.example.shushiyunsst.fragment.main.mine.child.ShareLabel
import com.example.wallet.db.UserInfoManager
import com.google.android.material.tabs.TabLayout
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 分享
 * */
class Miner22ShareFragment : SimpleListFragment<Miner22ShareBean>(),
    TabLayout.OnTabSelectedListener , ShareLabel {

    companion object {
        fun newInstance(): Miner22ShareFragment {
            val args = Bundle()

            val fragment = Miner22ShareFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun labelInfo(): String {
        return "推广粉丝"
    }

    private var currentType = Miner22ShareBean.valid.toString()

    private lateinit var mTabLayout: TabLayout


    private val listMap = mapOf(
        Miner22ShareBean.FANS.toString() to mutableListOf<Miner22ShareBean>(),
        Miner22ShareBean.REAL_NAME.toString() to mutableListOf<Miner22ShareBean>(),
        Miner22ShareBean.valid.toString() to mutableListOf<Miner22ShareBean>()
    )

    private val tableTitle = mapOf(
        Miner22ShareBean.valid.toString() to "有效",
        Miner22ShareBean.FANS.toString() to "粉丝",
        Miner22ShareBean.REAL_NAME.toString() to "实名",
    )

    private var requestFromNet = false

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
//        addHeader(R.layout.comment_header_layout)
//        mHeader.findViewById<View>(R.id.headerLayout)
//            .setBackgroundResource(R.color.bg_color_0)
//        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
//        mToolbar.title = "分享"
//        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        addHeader(R.layout.miner_22_header_share)

        mTabLayout = mHeader.findViewById(R.id.mTabLayout)

        for (e in tableTitle) {

            mTabLayout.addTab(
                mTabLayout.newTab()
                    .setText(e.value)
                    .setTag(e.key)
            )
        }
        mTabLayout.addOnTabSelectedListener(this)

        mRecyclerView.setPadding(0,PixUtils.dp2px(10),0,0)

        mRecyclerView.clipToPadding=false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration = false
        mSuperBackPressedSupport=false
        isEnableLoadMore = false
    }

    override fun getItemLayout(): Int {
        return R.layout.miner_22_share_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: Miner22ShareBean?) {
        helper?.setText(R.id.phoneTv,  "(${item?.accountHid})")
            ?.setText(R.id.accountName, item?.realName)
            ?.setText(R.id.iconTv, when(item?.level){
                Miner22ShareBean.FANS->"粉丝"
                Miner22ShareBean.valid->"有效"
                Miner22ShareBean.REAL_NAME->"实名"
                else -> ""
            })
            ?.setImageResource(
                R.id.icon,
                when (item?.level) {
                    Miner22ShareBean.FANS -> R.mipmap.dwni
                    Miner22ShareBean.valid -> R.mipmap.youxiao
                    Miner22ShareBean.REAL_NAME -> R.mipmap.shiming
                    else -> R.mipmap.youxiao
                }
            )
            ?.setTextColorRes(
                R.id.accountName,
                when (item?.level) {
                    Miner22ShareBean.FANS  -> R.color.PrimaryColor
                    else -> R.color.text_color_6
                }
            )
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(Miner22ShareBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val jsonFromRaw = ""
//        parseDate(jsonFromRaw)
//        if (!requestFromNet) {
            fromNet()
//            requestFromNet = true
//        } else {
//            fromNative()
//        }

    }

    private fun fromNative() {
        pageIndex = 1
        val list = listMap[currentType]
        loadListDate(list)
    }

    private fun fromNet() {
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

        val map = mapOf(
            "uid" to userInfo?.uid,
            "type" to "0",
            "invitationCode" to userInfo?.invitationCode
        )

        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryShareLog)
            .success {
                parseDate(it)
            }
            .failure {
                onFailed()
            }
            .error { _, _ ->
                onFailed()
            }.build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val list = obj["list"]

        val listStr = list?.toString() ?: ""
        val array = JSON.parseArray(listStr, Miner22ShareBean::class.java)

        loadListDate(array)
//        for (e in array) {
//            listMap[e.level.toString()]?.add(e)
//        }
//
//        for (e in 0..mTabLayout.tabCount) {
//            val tabAt = mTabLayout.getTabAt(e)
//            val text = tabAt?.text
//            val tag = tabAt?.tag
//            mTabLayout.getTabAt(e)?.text = "$text (${listMap[tag.toString()]?.size})"
//        }
//
//        fromNative()
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        currentType = tab?.tag.toString()
        if (!mSwipeRefreshLayout.isRefreshing) {
            mSwipeRefreshLayout.isRefreshing = true
        }
        onRefresh()
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {

    }

    override fun onTabReselected(tab: TabLayout.Tab?) {

    }


}